package com.dolphin.servicemap.service;

import com.dolphin.common.constant.AmapConfigConstants;
import com.dolphin.common.constant.CommonStatusEnum;
import com.dolphin.common.dto.DicDistrict;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.servicemap.mapper.DicDistrictMapper;
import com.dolphin.servicemap.remot.MapDicDistrictClient;
import com.mysql.cj.xdevapi.JsonArray;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public ResponseResult initDicDistrict(String keywords){

        //请求地图
        String dicDistrictResult = mapDicDistrictClient.dicDistrict(keywords);
        System.out.println(dicDistrictResult);

        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrictResult);
        int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }

        JSONArray countryDistrictJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);

        //国家
        for (int country = 0; country < countryDistrictJsonArray.size(); country++) {
            JSONObject countryDistrictJsonObject = countryDistrictJsonArray.getJSONObject(country);
            String countryAddressCode = countryDistrictJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryName = countryDistrictJsonObject.getString(AmapConfigConstants.NAME);
            String countryParentAddressCode = "0";
            String countryLevel = countryDistrictJsonObject.getString(AmapConfigConstants.LEVEL);

            insertDicDistrict(countryAddressCode,countryName,countryParentAddressCode,countryLevel);

            JSONArray provinceDistrictsJsonArray = countryDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            //省
            for (int p = 0; p < provinceDistrictsJsonArray.size(); p++) {
                JSONObject provinceDistrictJsonObject = provinceDistrictsJsonArray.getJSONObject(p);
                String provinceAddressCode = provinceDistrictJsonObject.getString(AmapConfigConstants.ADCODE);
                String provinceName = provinceDistrictJsonObject.getString(AmapConfigConstants.NAME);
                String provinceParentAddressCode =countryAddressCode;
                String provinceLevel = provinceDistrictJsonObject.getString(AmapConfigConstants.LEVEL);

                insertDicDistrict(provinceAddressCode,provinceName,provinceParentAddressCode,provinceLevel);

                JSONArray cityDistrictsJsonArray = provinceDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                //市
                for (int city = 0; city < cityDistrictsJsonArray.size(); city++) {
                    JSONObject cityDistrictJsonObject = cityDistrictsJsonArray.getJSONObject(city);
                    String cityAddressCode = cityDistrictJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityName = cityDistrictJsonObject.getString(AmapConfigConstants.NAME);
                    String cityParentAddressCode =provinceAddressCode;
                    String cityLevel = cityDistrictJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDicDistrict(cityAddressCode,cityName,cityParentAddressCode,cityLevel);

                    JSONArray districtsJsonArray = cityDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    //区
                    for (int d = 0; d < districtsJsonArray.size(); d++) {
                        JSONObject districtJsonObject = districtsJsonArray.getJSONObject(d);
                        String dAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String dName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String dParentAddressCode =cityAddressCode;
                        String dLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        if (dLevel.equals(AmapConfigConstants.STREET)) continue;

                        insertDicDistrict(dAddressCode,dName,dParentAddressCode,dLevel);


                    }
                }
            }
        }

        //插入数据库

        return ResponseResult.success(dicDistrictResult);
    }

    private void insertDicDistrict(String addressCode, String name, String parentAddressCode, String level) {
        DicDistrict district = new DicDistrict();
        district.setAddressCode(addressCode);
        district.setAddressName(name);
        int levelInt = generateLevel(level);
        district.setLevel(levelInt);

        district.setParentAddressCode(parentAddressCode);
        dicDistrictMapper.insert(district);
    }

    public int generateLevel(String level) {
        int levelInt = 0;
        if (level.trim().equals("country")) {
            levelInt = 0;
        } else if (level.trim().equals("province")) {
            levelInt = 1;
        } else if (level.trim().equals("city")) {
            levelInt = 2;
        } else if (level.trim().equals("district")) {
            levelInt = 3;
        }
        return levelInt;
    }
}
